<template>
    <div>
        <div class="container">
            <div class="handle-box">
                <el-input v-model="query.keyword" placeholder="资讯关键词 / 查询" class="handle-input mr10" clearable></el-input>
                <el-button type="primary" class="mr10" icon="el-icon-search" @click="func_handleSearch" plain>搜索</el-button>
                <el-button type="danger" class="mr10" icon="el-icon-lx-add" @click="func_handleAdd" plain>新建栏目</el-button>
				<el-dropdown :hide-on-click="false">
                    <el-button class="green mr10" plain>操作</el-button>
					<template #dropdown>
						<el-dropdown-menu>
                            <el-dropdown-item type="text" @click="func_handleBatchDel" class="red "> 批量删除</el-dropdown-item>
							<el-dropdown-item type="text" @click="func_handleBatchIsShow(1)" class="green "> 显示</el-dropdown-item>
							<el-dropdown-item type="text" @click="func_handleBatchIsShow(0)" class="green "> 不显示</el-dropdown-item>
							<el-dropdown-item type="text" @click="func_handleBatchIsSplit(1)" class="green "> 设定独立栏目 </el-dropdown-item>
							<el-dropdown-item type="text" @click="func_handleBatchIsSplit(0)" class="green "> 设定非独立栏目 </el-dropdown-item>
						</el-dropdown-menu>
					</template>
				</el-dropdown>
            </div>
            <el-table :data="newsCatalogTableData" border class="table" header-cell-class-name="table-header"  @selection-change="handleSelectionChange">
				<el-table-column type="selection" width="55" />
                <el-table-column prop="id" label="ID" width="55" align="center" v-if="false" />
				<el-table-column label="序号" width="80" align="center"><template #default="scope">
						{{scope.$index + 1}}
				</template></el-table-column>
                <el-table-column prop="catalogName" label="栏目名称" align="left" />
				<el-table-column prop="catalogEnname" label="栏目英文字" align="left" />
				<el-table-column prop="isShow" label="是否显示" >
					<template #default="scope">
						{{scope.row.isShow === null ? "否" : scope.row.isShow === 1 ? "显示": "不显示" }}
					</template>
				</el-table-column>
				<el-table-column prop="isSplit" label="是否独立" >
					<template #default="scope">
						{{scope.row.isSplit === null ? 0 : scope.row.isSplit === 1 ? "分割独立": "不独立" }}
					</template>
				</el-table-column>
                <el-table-column prop="isOnly" label="是否热门栏目" >
					<template #default="scope">
						{{scope.row.isOnly === null ? 0 : scope.row.isOnly === 1 ? "是": "否" }}
					</template>
				</el-table-column>
				<el-table-column prop="sort" label="数据排序" />
                <el-table-column label="操作" :width="optColumnWidth" align="center" >
                    <template #default="scope">
                        <el-button type="text" icon="el-icon-delete" class="red mr10" @click="func_handleDelete(scope.$index, scope.row)">删除</el-button>
                        <el-dropdown :hide-on-click="false">
                            <span class="el-dropdown-link green mr10"><el-icon class="el-icon-lx-info mr10" />更多</span>
                            <template #dropdown>
                                <el-dropdown-menu>
                                    <el-dropdown-item type="text" @click="func_handleEdit(scope.$index, scope.row)" class="green "> 编辑 </el-dropdown-item>

                                    <el-dropdown-item type="text" @click="func_handleEditHot(scope.$index, scope.row)" class="green "> 加入热门推荐 </el-dropdown-item>
                                </el-dropdown-menu>
                            </template>
                        </el-dropdown>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination background layout="total, prev, pager, next" :current-page="query.pageNum"
                    :page-size="query.pageSize" :total="dataTotal" @current-change="func_handlePageChange"></el-pagination>
            </div>
        </div>
        <!-- ---------------------------------------------------------------------------------------------------------------------- -->
        <!-- 编辑弹出框 -->
        <el-dialog title="新建资讯栏目" v-model="vNewsCatalogDialogVisible" width="55%" :close-on-click-modal="false">
            <VNewsCatalogForm v-bind:selectValue="vNewsCatalogData" @newsCatalogData="func_loadvNewsCatalogData" />
        </el-dialog>
    </div>
</template>

<script>
import { ref, reactive, toRefs } from "vue";
import { useRouter, useRoute } from 'vue-router';
import { ElMessage, ElMessageBox } from "element-plus";
import { findNewsCatalogListPage, batchIsShow, batchIsSplit, deleteNewsCatalog, batchDeleteNewsCatalog, setOnlyFlg } from "../../../api/hairmanager/newsCatalog";
import VNewsCatalogForm from "./components/newsCatalogForm.vue";
import { NO_DATA_SELECT_ERROR, SUCCESS_DELDATA_DIALOG_MESSAGE, SUCCESS_EDITDATA_DIALOG_MESSAGE } from "../../../api/CommonErrorMessage";

export default {
    name: "newsCatalog",
    components: {VNewsCatalogForm},
    setup(props, cex){
        const router = useRouter();
		const route = useRoute();
        const multipleSelection = ref([])

        //主数据条件查询
        const query = reactive({
            keyword: "",
			groupId: 0,
            pageIndex: 1,
            pageSize: 10,
        });
        // 主数据总页数
        const dataTotal = ref(0);
        // 主数据表格
        let newsCatalogTableData = ref([]);
        const optColumnWidth = 150;
        // =========================================================================================
        // 主业务处理 ========================================================================================= START
        /**
         * @name 获取表格数据
         */
        const func_getData = () => {
            findNewsCatalogListPage(query).then((res) => {
                newsCatalogTableData.value = res.data.list;
                dataTotal.value = res.data.total;
            });
        };
        func_getData();
        /**
         * @name 主数据查询操作
         */
        const func_handleSearch = () => {
            query.pageNum = 1;
            func_getData();
        };
        /**
         * @name 分页导航
         */
        const func_handlePageChange = (val) => {
            // query.pageIndex = val;
            query['pageNum'] = val;
            func_getData();
        };
		/**
         * @name 主数据编辑对话框操作
         */
        const func_handleAdd = () => {
			vNewsCatalogData.value['load'] = false;
            vNewsCatalogData.value['id'] = 0;
            vNewsCatalogDialogVisible.value = true;
        };
        /**
         * @name 读取新闻资讯详细数据
         */
        let idx = -1;
        const func_handleEdit = (index, row) => {
            idx = index;
			vNewsCatalogData.value['load'] = true;
            vNewsCatalogData.value['id'] = row['id'];
            vNewsCatalogDialogVisible.value = true;
        };
        /**
         * @name 绑定唯一热门栏目
         */
        const func_handleEditHot = (index, row) => {
            let obj = {id: row['id'], isOnly: 1};
			setHot(obj).then((res) =>{
                func_getData();
            });
        };
        const func_handleHotNews = () => {
            vHotNewsDialogVisible.value = true;
        };
        /**
         * @name 删除新闻资讯数据操作
         */
        const func_handleDelete = (index, row) => {
            // 二次确认删除
            ElMessageBox.confirm("确定要删除吗？", "提示", { type: "warning", }).then(() => {
                func_delDialogEdit(row['id']);
            }).catch(() => {});
        };
        /**
         * @name 行内数据状态变更
         */
		const func_changeRowStatus = (index, row) => {
			if(index === -1){
				return;
			}else{
				if(index > -1){
					let obj = {'id':row['id'], 'disable':row['disable']};
					configServiceProjectDisable(obj).then((res) => {
						if(res.code != 200) {
							if(res.type == 0) {
								ElMessage.warning(res.message);
							} else {
								ElMessage.error(res.message);
							}
						} else {
							func_getData();
							ElMessage.success(SUCCESS_EDITDATA_DIALOG_MESSAGE);
						}
					});
				}
			}
		};
        /**
         * @name 删除新闻资讯配置
         */
        const func_delDialogEdit = (id) => {
            deleteNewsCatalog(id).then((res) => {
                if(res.code != 200) {
                    if(res.type == 0) {
                        ElMessage.warning(res.message);
                    } else {
                        ElMessage.error(res.message);
                    }
                } else {
                    ElMessage.success(SUCCESS_DELDATA_DIALOG_MESSAGE);
                    func_getData(); // 刷新数据
                }
            });
        };
        /**
         * @name 是否显示
         */
        const func_handleBatchIsShow = (v) => {
            if(multipleSelection.value.length ===0){
                ElMessage.warning(NO_DATA_SELECT_ERROR);
                return false;
            }
            var obj = { ids: multipleSelection.value, isShowFlg: v };
            batchIsShow(obj).then((res) => {
                if(res.code != 200) {
                    if(res.type == 0) {
                        ElMessage.warning(res.message);
                    } else {
                        ElMessage.error(res.message);
                    }
                } else {
                    ElMessage.success(SUCCESS_EDITDATA_DIALOG_MESSAGE);
                    func_getData(); // 刷新数据
                }
            });
        };
        /**
         * @name 是否分割
         */
        const func_handleBatchIsSplit = (v) => {
            if(multipleSelection.value.length ===0){
                ElMessage.warning(NO_DATA_SELECT_ERROR);
                return false;
            }
            var obj = { ids: multipleSelection.value, isSplit: v };
            batchIsSplit(obj).then((res) => {
                if(res.code != 200) {
                    if(res.type == 0) {
                        ElMessage.warning(res.message);
                    } else {
                        ElMessage.error(res.message);
                    }
                } else {
                    ElMessage.success(SUCCESS_EDITDATA_DIALOG_MESSAGE);
                    func_getData(); // 刷新数据
                }
            });
        };
        /**
         * @name 批次删除数据
         */
        const func_handleBatchDel = () => {
            if(multipleSelection.value.length ===0){
                ElMessage.warning(NO_DATA_SELECT_ERROR);
                return false;
            }
            var obj = multipleSelection.value;
            batchDeleteNewsCatalog(obj).then((res) => {
                if(res.code != 200) {
                    if(res.type == 0) {
                        ElMessage.warning(res.message);
                    } else {
                        ElMessage.error(res.message);
                    }
                } else {
                    ElMessage.success(SUCCESS_DELDATA_DIALOG_MESSAGE);
                    func_getData(); // 刷新数据
                }
            });
        }
        const handleSelectionChange = (val) => {
            let ids = [];
            val.forEach((element) => {
                ids.push(element['id']);
            });
            multipleSelection.value = ids;
        };
        // 主业务处理 ========================================================================================= END
        // 业务插件处理 ========================================================================================= START
        const vNewsCatalogDialogVisible = ref(false);
        const vNewsCatalogData = ref({
            load: false,
            id: 0,
        });
        const func_loadvNewsCatalogData = (v) => {
            if(v['state'] === 1) {
                vNewsCatalogDialogVisible.value = false;
                func_getData();
            } else {
                vNewsCatalogDialogVisible.value = true;
            }
        };
        // 业务插件处理 ========================================================================================= END
        return {
            // 系统环境变量
            optColumnWidth, query, dataTotal, multipleSelection,
            // 主业务处理
            func_getData, newsCatalogTableData, func_handleAdd, func_handleEdit, func_handleDelete, func_handleSearch, func_handlePageChange, func_handleEditHot,
            func_delDialogEdit, func_changeRowStatus, func_handleBatchIsShow, func_handleBatchIsSplit, func_handleBatchDel, handleSelectionChange,
            // 业务插件处理
            vNewsCatalogDialogVisible, vNewsCatalogData, func_loadvNewsCatalogData,
        };
    },
};
</script>
<style scoped>
.handle-box {
    margin-bottom: 20px;
}
.handle-select {
    width: 120px;
}
.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.green {
    color: #67c23a;
}
.blue {
    color: #1684FC;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
